//
//  SafeCenterViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/8/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "SafeCenterViewController.h"
#import "BoundWXController.h"
#import "BoundAlipayController.h"

@interface SafeCenterViewController ()

@property (nonatomic, strong) UIImageView   *weixin;
@property (nonatomic, strong) UIImageView   *zhifubao;
@property (nonatomic, strong) UILabel       *weixinLabel;
@property (nonatomic, strong) UILabel       *zhifubaoLabel;
@property (nonatomic, strong) UIImageView   *weixinImg;
@property (nonatomic, strong) UIImageView   *zhifubaoImg;
@property (nonatomic, assign) NSInteger     type;
@property (nonatomic, strong) NSString      *aliPayNum;
@property (nonatomic, strong) NSString      *weiXinNum;
@property (nonatomic, assign) BOOL          isWeiXin;
@property (nonatomic, assign) BOOL          isAliPay;

@end

@implementation SafeCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"安全中心";
    _type = 0;
    _isWeiXin = NO;
    _isAliPay = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
}

- (void)setUI {
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(15), ScreenX375(150), ScreenX375(18))];
    noteLabel.text = @"注：绑定打钩";
    noteLabel.textColor = FONTCOLOR_BLACK;
    noteLabel.font = Kfont(15);
    [self.view addSubview:noteLabel];
    
    
    _weixin = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(50), ScreenX375(80), ScreenX375(100), ScreenX375(100))];
    
    _weixinLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(26), ScreenX375(9), ScreenX375(45), ScreenX375(14))];
    _weixinLabel.text = @"未绑定";
    _weixinLabel.textColor = REXADECIMALCOLOR(@"#888888");
    _weixinLabel.font = Kfont(14);
    _weixinLabel.textAlignment = NSTextAlignmentLeft;
    [_weixin addSubview:_weixinLabel];
    
    _weixinImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(28), ScreenX375(40), ScreenX375(44), ScreenX375(44))];
    _weixinImg.image = [UIImage imageNamed:@"矢量智能对象"];
    [_weixin addSubview: _weixinImg];
    
    [self setWeiXinView];
    [self.view addSubview:_weixin];
    
    _zhifubao = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(225), ScreenX375(80), ScreenX375(100), ScreenX375(100))];
    
    _zhifubaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(26), ScreenX375(9), ScreenX375(45), ScreenX375(14))];
    _zhifubaoLabel.text = @"已绑定";
    _zhifubaoLabel.font = Kfont(14);
    _zhifubaoLabel.textColor = REXADECIMALCOLOR(@"#888888");
    _zhifubaoLabel.textAlignment = NSTextAlignmentLeft;
    [_zhifubao addSubview:_zhifubaoLabel];
    
    _zhifubaoImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(28), ScreenX375(40), ScreenX375(44), ScreenX375(44))];
    _zhifubaoImg.image = [UIImage imageNamed:@"支付宝1"];
    [_zhifubao addSubview: _zhifubaoImg];
    
    [self setZhifubaoView];
    [self.view addSubview:_zhifubao];
    
    UIButton *sureBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(50), ScreenX375(200), ScreenX375(100), ScreenX375(36))];
    sureBtn1.tag = 103;
    [sureBtn1 setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [sureBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn1 addTarget:self action:@selector(turnToWX) forControlEvents:UIControlEventTouchUpInside];
    sureBtn1.titleLabel.font = Kfont(16);
    sureBtn1.layer.cornerRadius = ScreenX375(18);
    sureBtn1.clipsToBounds = YES;
    [self.view addSubview:sureBtn1];
    
    UIButton *sureBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(225), ScreenX375(200), ScreenX375(100), ScreenX375(36))];
    sureBtn2.tag = 104;
    [sureBtn2 setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [sureBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn2 addTarget:self action:@selector(turnToAliPay) forControlEvents:UIControlEventTouchUpInside];
    sureBtn2.titleLabel.font = Kfont(16);
    sureBtn2.layer.cornerRadius = ScreenX375(18);
    sureBtn2.clipsToBounds = YES;

    [self.view addSubview:sureBtn2];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)getData {
    NSDictionary *parameter = @{@"id":kNSUDefaultReadKey(@"userId"),@"userId":kNSUDefaultReadKey(@"userId")};
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getUserInfo",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            NSString *aliPay = [[NSString alloc] init];
            aliPay = successResponse[@"data"][@"userInfo"][@"aliAccount"];
            if ([aliPay class] == [NSNull class]) {
                self.isAliPay = NO;
            }else if ([aliPay isEqualToString:@"-1"]) {
                self.isAliPay = NO;
            }else{
                self.aliPayNum = aliPay;
                self.isAliPay = YES;
            }
            NSString *weiXin = [[NSString alloc] init];
            weiXin = successResponse[@"data"][@"userInfo"][@"wxAccount"];
            if ([weiXin class] == [NSNull class]) {
                self.isWeiXin = NO;
            }else if ([weiXin isEqualToString:@"-1"] || weiXin == nil) {
                self.isWeiXin = NO;
            }else{
                self.weiXinNum = weiXin;
                self.isWeiXin = YES;
            }
            [self setUI];
            [self setButton];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)setWeiXinView{
    if (_isWeiXin) {
        _weixin.image = [UIImage imageNamed:@"组55"];
        _weixin.layer.borderWidth = 0;
        _weixinLabel.textColor = REXADECIMALCOLOR(@"#f21934");
        _weixinLabel.text = @"已绑定";
    }else{
        _weixin.layer.cornerRadius = ScreenX375(6);
        _weixin.layer.borderWidth = 1.0;
        _weixin.layer.borderColor = REXADECIMALCOLOR(@"#bbbbbb").CGColor;
        _weixin.image = nil;
        _weixinLabel.textColor = REXADECIMALCOLOR(@"#888888");
        _weixinLabel.text = @"未绑定";
    }
}

-(void)setZhifubaoView{
    if (_isAliPay) {
        _zhifubao.image = [UIImage imageNamed:@"组55"];
        _zhifubao.layer.borderWidth = 0;
        _zhifubaoLabel.textColor = REXADECIMALCOLOR(@"#f21934");
        _zhifubaoLabel.text = @"已绑定";
    }else{
        _zhifubao.layer.cornerRadius = ScreenX375(6);
        _zhifubao.layer.borderWidth = 1.0;
        _zhifubao.layer.borderColor = REXADECIMALCOLOR(@"#bbbbbb").CGColor;
        _zhifubao.image = nil;
        _zhifubaoLabel.textColor = REXADECIMALCOLOR(@"#888888");
        _zhifubaoLabel.text = @"未绑定";
    }
}

-(void)setButton{
    UIButton *button1 = (UIButton *)[self.view viewWithTag:103];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:104];
    if (!_isWeiXin) {
        [button1 setTitle:@"绑定" forState:UIControlStateNormal];
    }else{
        [button1 setTitle:@"解除绑定" forState:UIControlStateNormal];
    }
    if (!_isAliPay) {
        [button2 setTitle:@"绑定" forState:UIControlStateNormal];
    }else{
        [button2 setTitle:@"解除绑定" forState:UIControlStateNormal];
    }
}

-(void)turnToWX{
    BoundWXController *vc = [[BoundWXController alloc] init];
    vc.isWeiXin = _isWeiXin;
    vc.wxAccount = [_weiXinNum mutableCopy];
    vc.result = ^(BOOL result,NSString *change){
        self.isWeiXin = result;
        self.weiXinNum = change;
        [self setWeiXinView];
        [self setButton];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)turnToAliPay{
    BoundAlipayController *vc = [[BoundAlipayController alloc] init];
    vc.isAliPay = _isAliPay;
    vc.aliPayNum = [_aliPayNum mutableCopy];
    vc.result = ^(BOOL result,NSString *change){
        self.isAliPay = result;
        self.aliPayNum = change;
        [self setZhifubaoView];
        [self setButton];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
